package com.project.mvprxsample.presenters;

import android.content.Context;

import com.project.mvprxsample.bean.BaseResult;
import com.project.mvprxsample.bean.NetworkErrorBean;
import com.project.mvprxsample.net.BaseApi;
import com.project.mvprxsample.net.HttpStateCode;
import com.project.mvprxsample.net.MyObserver;
import com.project.mvprxsample.net.RetrofitUtils;
import com.project.mvprxsample.view.BaseView;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 基础控制器
 * Created by lvyang on 2017/05/12.
 */
public abstract class BasePresenter<V> {
    public Context mContext;
    public V view;
    public BaseApi mBaseApi;
    public RetrofitUtils mRetrofitUtils;

    public BasePresenter(Context context, V view) {
        this.mContext = context;
        mRetrofitUtils = new RetrofitUtils(context);
        mBaseApi = mRetrofitUtils.getRetrofit().create(BaseApi.class);
        initView(view);
        initModel();
    }

    public void initView(V view) {
        this.view = view;
    }

    public abstract void initModel();


    public <T> void resultJSON(Observable<BaseResult<T>> api, MyObserver observer) {
        setSubscribe(api, observer);
    }

    public <T> void resultJSON(Observable<BaseResult<T>> api, final BaseView<T> view, final String tag) {
        setSubscribe(api, new MyObserver(mContext) {
            @Override
            public void onCompleted() {
                super.onCompleted();
                view.onCompleted(tag);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.getFail(new NetworkErrorBean(HttpStateCode.HTTP_ERROR_STATE, e.getMessage()), tag);
            }

            @Override
            public void onNext(BaseResult baseResult) {
                super.onNext(baseResult);
                if (baseResult.getStatusCode() == HttpStateCode.HTTP_SUEECSS_STATE) {
                    view.getSuccess((T) baseResult.getData(), tag);
                } else {
                    view.getFail(new NetworkErrorBean(baseResult.getStatusCode(), baseResult.getMessage()), tag);
                }
            }
        });
    }


//    public <T> void resultListJSON(Observable<BaseListResult<T>> api, MyObserver observer){
//        setSubscribe2(api,observer);
//    }

    /**
     * 插入观察者-泛型
     *
     * @param <T>
     * @param observable
     * @param observer
     */
    public <T> void setSubscribe(Observable<BaseResult<T>> observable, MyObserver observer) {
        observable.subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

//    /**
//     *  插入观察者-Object
//     * @param observable
//     * @param observer
//     */
//    public <T>  void setSubscribe2(Observable<BaseListResult<T>> observable, MyObserver observer) {
//        observable.subscribeOn(Schedulers.io())
//                .subscribeOn(Schedulers.newThread())//子线程访问网络
//                .observeOn(AndroidSchedulers.mainThread())//回调到主线程
//                .subscribe(observer);
//    }


}
